/*
 * Copyright (C) 2017 The MoonLake Authors
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


package com.minecraft.moonlake.legendaryweaponrel.weapons.armor;

import com.minecraft.moonlake.executor.Consumer;
import com.minecraft.moonlake.legendaryweaponrel.weapons.WeaponAbstractPlayerInteract;
import org.bukkit.Material;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemStack;

public abstract class WeaponAbstractArmorBase extends WeaponAbstractPlayerInteract implements WeaponArmor {

    public WeaponAbstractArmorBase(String type, String displayName, Material material) {
        super(type, displayName, material);
    }

    @Override
    public Consumer<PlayerInteractEvent> onConsumer(Player player) {
        // 重写武器抽象玩家交互的消费函数跳过权限处理直接调用 onInteract 函数
        return event -> onInteract(event, player);
    }

    @Override
    protected void onInteract(PlayerInteractEvent event, Player player) {
        if(!checkPermission(player)) {
            // 没有权限则阻止玩家与传说武器的护甲交互 (防止穿戴)
            event.setCancelled(true);
            event.setUseItemInHand(Event.Result.DENY);
            event.setUseInteractedBlock(Event.Result.DENY);
            playerUpdateInventory(event.getPlayer());
        }
    }

    @Override
    public Consumer<InventoryClickEvent> onConsumerClick(Player player) {
        // 处理护甲在物品栏点击的事件
        return event -> onClick(event, player, event.getCurrentItem());
    }

    protected void onClick(InventoryClickEvent event, Player player, ItemStack weaponItemStack) {
        // 判断玩家是否拥有权限则允许点击
        if(!checkPermission(player)) {
            // 没权限则阻止
            event.setCancelled(true);
            event.setResult(Event.Result.DENY);
        }
    }

    public Consumer<EntityDamageByEntityEvent> onConsumerArmor(Player player) {
        // 处理护甲穿戴者玩家的受伤事件
        return event -> onDamage(event, player, (LivingEntity) event.getDamager());
    }

    protected abstract void onDamage(EntityDamageByEntityEvent event, Player player, LivingEntity dmg);
}
